package com.ftg.learn.common.utils;

import java.util.regex.Pattern;

public class PasswordValidator {
    // 密码最小长度
    private final int minLength;
    // 密码最大长度
    private final int maxLength;
    // 是否要求包含小写字母
    private final boolean requireLowerCase;
    // 是否要求包含大写字母
    private final boolean requireUpperCase;
    // 是否要求包含数字
    private final boolean requireDigit;
    // 是否要求包含特殊字符
    private final boolean requireSpecialChar;
    // 特殊字符范围
    private final String specialChars;

    private PasswordValidator(Builder builder) {
        this.minLength = builder.minLength;
        this.maxLength = builder.maxLength;
        this.requireLowerCase = builder.requireLowerCase;
        this.requireUpperCase = builder.requireUpperCase;
        this.requireDigit = builder.requireDigit;
        this.requireSpecialChar = builder.requireSpecialChar;
        this.specialChars = builder.specialChars;
    }

    // 验证密码是否符合要求
    public boolean validate(String password) {
        if (password == null || password.length() < minLength || password.length() > maxLength) {
            return false;
        }

        if (requireLowerCase && !containsLowerCase(password)) {
            return false;
        }

        if (requireUpperCase && !containsUpperCase(password)) {
            return false;
        }

        if (requireDigit && !containsDigit(password)) {
            return false;
        }

        if (requireSpecialChar && !containsSpecialChar(password)) {
            return false;
        }

        return true;
    }

    private boolean containsLowerCase(String password) {
        return Pattern.compile("[a-z]").matcher(password).find();
    }

    private boolean containsUpperCase(String password) {
        return Pattern.compile("[A-Z]").matcher(password).find();
    }

    private boolean containsDigit(String password) {
        return Pattern.compile("[0-9]").matcher(password).find();
    }

    private boolean containsSpecialChar(String password) {
        String pattern = "[" + Pattern.quote(specialChars) + "]";
        return Pattern.compile(pattern).matcher(password).find();
    }

    // 构建器模式
    public static class Builder {
        //  默认参数密码最小长度为5
        private int minLength = 5;
        //  默认参数密码最大长度为15
        private int maxLength = 15;
        //  默认参数是否要求包含小写字母为true
        private boolean requireLowerCase = false;
        //  默认参数是否要求包含大写字母为false
        private boolean requireUpperCase = false;
        //  默认参数是否要求包含数字为true
        private boolean requireDigit = false;
        //  默认参数是否要求包含特殊字符为false
        private boolean requireSpecialChar = false;
        private String specialChars = "!@#$%^&*()-_=+[]{}|;:'\",.<>/?";

        public Builder minLength(int minLength) {
            this.minLength = minLength;
            return this;
        }

        public Builder maxLength(int maxLength) {
            this.maxLength = maxLength;
            return this;
        }

        public Builder requireLowerCase(boolean requireLowerCase) {
            this.requireLowerCase = requireLowerCase;
            return this;
        }

        public Builder requireUpperCase(boolean requireUpperCase) {
            this.requireUpperCase = requireUpperCase;
            return this;
        }

        public Builder requireDigit(boolean requireDigit) {
            this.requireDigit = requireDigit;
            return this;
        }

        public Builder requireSpecialChar(boolean requireSpecialChar) {
            this.requireSpecialChar = requireSpecialChar;
            return this;
        }

        public Builder specialChars(String specialChars) {
            this.specialChars = specialChars;
            return this;
        }

        public PasswordValidator build() {
            return new PasswordValidator(this);
        }
    }
}
